<template>
  <div class="content">
    <!-- 左侧滚动条 -->
    <div class="left">
      <div v-for="item in categories" :key="item.id" :class="{'left__item': true, 'left__item--active': checked === item.id }" @click="handleChange(item)">
      {{item.name}}
      </div>
    </div>
    <div class="product">
      <div class="product__item" v-for="item in categoriesList" :key="item._id">
        <img class="product__item__img" :src="item.imgUrl" alt="">
        <div class="product__item__detail">
          <h4 class="product__item__title">{{ item.name }}</h4>
          <p class="product__item__sales">月售{{ item.sales }}件</p>
          <p class="product__item__price">
            <span class="product__item__yen">&yen;{{ item.price }}</span>
            <span class="product__item__origin">&yen;{{ item.oldPrice }}</span>
          </p>
        </div>
        <div class="product__number">
          <span class="product__number__minus" @click="handldGoodsCount(shopId, item._id, item, -1)">-</span>
          <!-- 计算器中的数量从 vuex 中获取，一开始没存入 vuex 展示 0 -->
          {{ cartList?.[shopId]?.[item._id]?.count || 0 }}
          <span class="product__number__plus" @click="handldGoodsCount(shopId, item._id, item, 1)">+</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive, watchEffect, toRefs } from 'vue'
import { useRoute } from 'vue-router'
import { goodsInfo } from '../../api/detail'
import { useGoodsCountEffect } from '../../commonEffect/cartEffect'
// 左侧商品分类
const categories = [
  { id: 0, name: '全部商品全部', tab: 'all' },
  { id: 1, name: '秒杀', tab: 'seckill' },
  { id: 2, name: '新鲜水果', tab: 'fruit' }]

// 切换左侧分类 发起axios请求 更新右侧具体商品
function useGoodsInfoEffect (shopId) {
  const data = reactive({ categoriesList: [] })
  const getGoodsInfo = async (tab) => {
    const res = await goodsInfo(shopId, { tab })
    console.log(res.data)
    data.categoriesList = res.data
  }
  const { categoriesList } = toRefs(data)

  return { categoriesList, getGoodsInfo }
}
// 点击切换cate 1：改变checked，切换类名 2：将tab传出去
function useChangeCateEffect () {
  const checked = ref(0)
  const cateTab = ref(categories[0].tab) // 动态获取而不是简单赋值 'all'
  const handleChange = (item) => {
    checked.value = item.id
    cateTab.value = item.tab
  }
  return { checked, cateTab, handleChange }
}

export default {
  name: 'Content',
  setup () {
    const shopId = ref(0)
    const route = useRoute()

    shopId.value = route.params.id
    const { checked, cateTab, handleChange } = useChangeCateEffect()
    const { categoriesList, getGoodsInfo } = useGoodsInfoEffect(shopId.value)
    watchEffect(() => {
      getGoodsInfo(cateTab.value)
    })
    const { cartList, handldGoodsCount } = useGoodsCountEffect()
    return { shopId, checked, categories, categoriesList, cartList, handleChange, handldGoodsCount }
  }
}
</script>

<style lang="scss" scoped>
.content {
  display: flex;
  border-top: .01rem solid #ddd;
   .left{
    display: flex;
    flex-direction: column;
    width: 0.76rem;
    height: 100%;
    background-color: #f5f5f5;
    &__item {
      padding:.2rem 0;
      font-size: .16rem;
      line-height: .2rem;
      color: #333;
      &--active {
        color: #000;
        background: #fff;
      }
    }

  }
  .product {
    overflow-y: scroll;
    flex: 1;
    &__item {
      display: flex;
      position: relative;
      margin: 0 .16rem;
      padding: .12rem 0;
      border-bottom: .01rem solid #f1f1f1;
      &__img {
        width: 0.68rem;
        height: 0.68rem;
        margin-right: .16em;
      }
      &__title {
        margin: 0;
        line-height: 0.2rem;
        font-size: 0.14rem;
        color: #333;
      }
      &__sales {
        margin: .06rem 0;
        color: #333;
        font-size: .16rem;
      }
      &__price {
        margin: 0;
        line-height: 0.2rem;
        font-size: 0.15rem;
        color: #e33030;
      }
      &__origin {
        margin-left: .07rem;
        font-size: .12rem;
        color: #ef8c8c;
        text-decoration: line-through;
      }
      .product__number {
        position: absolute;
        right: 0.18rem;
        bottom: 0.12rem;
        display: flex;
        align-items: center;
        font-size: .2rem;
        &__minus, &__plus {
          display: inline-block;
          width: 0.16rem;
          height: 0.16rem;
          line-height: .2rem;
          text-align: center;
          color: #666;
          border: .01rem solid #666;
          border-radius: 50%;
          margin-right: .06rem;
        }
        &__plus {
          margin: 0 0 0 .06rem;
          color: #fff;
          background-color: #6ccde7;
        }
      }
    }
  }
}
</style>
